<?php
/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\traits;

use support\Response;

trait AdminAddTrait
{
    /**
     * 禁用数据
     * @return Response
     */
    public function add(): Response
    {
        $model = $this->model;
        $validate = $this->validate;
        $request = request();

        if (($request)->method() === 'POST') {
            $param = ($request)->all();

            $check = $validate->scene('admin_add')->check($param);
            if (!$check) {
                return admin_error($validate->getError());
            }

            $result = $model::create($param);

            $url = URL_BACK;
            if (isset($param['_create']) && (int)$param['_create'] === 1) {
                $url = URL_RELOAD;
            }
            return $result ? admin_success('添加成功', [], $url) : admin_error();
        }

        return $this->fetch();

    }
}